<?php
namespace Home\Controller;

use Admin\Controller\OrderController;
class MemberController extends CommonController{

	public function index(){
		
		$this->display();
	}
	public function info(){
		if(IS_POST){
			$curModel = D(CONTROLLER_NAME);
			$data = I('post.info');
			if(!$rs = $curModel->create($data))$this->error($curModel->getError());
			$result = $curModel->where("id='{$this->member_info['id']}'")->save($data);
			$result ? $this->success('保存成功！') : $this->error('保存失败！');
		}else{
			$this->display();
		}
	}
	public function bund(){
		$curModel = D('Member');
		if(IS_POST){
			$data = I('post.info');
			if($this->member_info['is_bund']=='1')$this->error('您已经绑定了账号!');
			if(!$rs = $curModel->create($data))$this->error($curModel->getError());
			$link_data = $this->getLinkId($data['mobile'], $data['password']);
			if(empty($link_data))$this->error('手机号或密码错误');
			//把接口取到的值赋值给对应字段
			$data['password'] = md5($data['password']);
			$data['is_bund'] = '1';
			$result = $curModel->add($data);
			$result ? $this->success('绑定成功！',U('index')) : $this->error('绑定失败！');
		}else{
			$this->display();
		}
	}
	
	public function cent_log(){
		$curModel = D('CentLog');
		$where['member_id'] = $this->member_info['id'];
		$order = "add_time desc";
		$p = I('page')?(int)I('page'):1;
		$pagesize = 10;
		$offset = ($p-1)*$pagesize;
		$datas = $curModel->field("left(add_time,10) as day")->where($where)->group("left(add_time,10)")->order($order)->limit("$offset,$pagesize")->select();
		foreach ($datas as $key=>$val){
			$logs = $curModel->where("left(add_time,10)='{$val['day']}' and member_id='{$this->member_info['id']}'")->order("add_time asc")->select();
			$total_cent = 0;
			foreach ($logs as $v){
				if($v['type']=='1'){
					$total_cent = $total_cent+$v['cent_value'];
				}elseif ($v['type']=='2'){
					$total_cent = $total_cent-$v['cent_value'];
				}
			}
			$datas[$key]['total_cent'] = $total_cent;
			$datas[$key]['logs'] = $logs;
			if($val['day'] == date('Y-m-d')){
				$datas[$key]['day'] = '今天';
			}else{
				$datas[$key]['day'] = date('m-d',strtotime($val['day']));
			}
		}
		$this->assign('datas',$datas);
		IS_POST ? $this->display('log_list') : $this->display();
	}
	private function getLinkId($mobile,$password){
		
		return 6;
	}
}